<?php
/**
* @file $Id: Inputs.php 500 2007-05-11 17:21:16Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/


/**
* Create a clickable one-line text input field
*
* @param string $value initial value in the text field (blank if the text field should be empty)
* @param string $name name of the HTML form field
* @param string $title optional title that is displayed under the field
* @param string $options optional set of options that are embedded into the <INPUT> tag
* @param string $div If false, the input field will be displayed by default (without the user clicking on the field). If true, the field will have to be clicked on before it can be edited unless $value is blank.  If set to 'force', the field will have to be clicked on even if blank (blank will be translated to a dash "-") 
* @return string HTML code for clickable text field
*/
function TextInput($value,$name,$title='',$options='',$div=true)
{	global $_FOCUS;

	if(Preferences('HIDDEN')!='Y')
		$div = false;

	// mab - support array style $option values
	if(AllowEdit() && !$_REQUEST['_FOCUS_PDF'])
	{
		$value1 = is_array($value) ? $value[1] : $value;
		$value = is_array($value) ? $value[0] : $value;

		if(strpos($options,'size')===false && $value!='' && strlen($value)<35)
			$options .= ' size='.ceil(strlen($value)*.85);
		elseif(strpos($options,'size')===false && $value!='')
			$options .= ' size=30';
		elseif(strpos($options,'size')===false)
			$options .= ' size=10';
		$comment = '<!-- '.$value.' -->';

		if(((trim($value)=='' && $div!=='force') || $div==false))
			return $comment."<INPUT type=text name=$name id=input$name ".(($value || $value==='0')?"value=\"".str_replace('"','\"',$value)."\"":'')." $options>".(($title!='')?"<BR><small>$title</small>":'');
		else
			return $comment."<DIV id='div$name'><div id='innerdiv$name' onclick='javascript:addHTML(\"<INPUT type=text id=input$name name=$name ".(($value || $value==='0')?"value=\\\"".str_replace("'",'"+single_quote+"',str_replace('"','"+double_quote+"',$value))."\\\"":'')." ".str_replace('"','\"',$options).">".(($title!='')?"<BR><small>$title</small>":'')."\",\"div$name\",true);document.getElementById(\"input$name\").value = replaceAll(document.getElementById(\"input$name\").value,\"``\",\"\\\"\");doOnresize();document.getElementById(\"input$name\").focus();'><span style='border-bottom-style:dotted;border-bottom-width:1;border-bottom-color:#0033CC;'>".(($value!='')?$value1:'-').'</span>'.(($title!='')?"<BR><small>$title</small>":'')."</div></DIV>";
	}
	else
		return (((is_array($value)?$value[1]:$value)!='')?(is_array($value)?$value[1]:$value):'-').(($title!='')?"<BR><small>$title</small>":'');
}

/**
* Create a clickable multi-line text input field
*
* @param string $value initial value in the text field (blank if the text field should be empty)
* @param string $name name of the HTML form field
* @param string $title optional title that is displayed under the field
* @param string $options optional set of options that are embedded into the <TEXTAREA> tag
* @param string $div If false, the input field will be displayed by default (without the user clicking on the field). If true, the field will have to be clicked on before it can be edited unless $value is blank.  If set to 'force', the field will have to be clicked on even if blank (blank will be translated to a dash "-") 
* @return string HTML code for clickable multi-line text field
*/
function TextAreaInput($value,$name,$title='',$options='',$div=true)
{	global $_FOCUS;

	if(Preferences('HIDDEN')!='Y')
		$div = false;

	if(AllowEdit() && !$_REQUEST['_FOCUS_PDF'])
	{
		$value = str_replace("'","&rsquo;",str_replace('"','&rdquo;',$value));

		if(strpos($options,'cols')===false)
			$options .= ' cols=30';
		if(strpos($options,'rows')===false)
			$options .= ' rows=4';
		$rows = substr($options,strpos($options,'rows')+5,2)*1;
		$cols = substr($options,strpos($options,'cols')+5,2)*1;

		if(($value=='' && $div!=='force') || $div==false)
			return "<TEXTAREA name=$name $options>$value</TEXTAREA>".(($title!='')?"<BR><small>$title</small>":'');
		else
			return "<DIV id='div$name'><div onclick='javascript:addHTML(\"<TEXTAREA id=textarea$name name=$name $options>".ereg_replace("[\n\r]",'\u000D\u000A',str_replace("\r\n",'\u000D\u000A',str_replace("'","&rsquo;",$value)))."</TEXTAREA>".(($title!='')?"<BR><small>".str_replace("'","&rsquo;",$title)."</small>":'')."\",\"div$name\",true); document.getElementById(\"textarea$name\").value=unescape(document.getElementById(\"textarea$name\").value);doOnresize();'><TABLE class=innerLO_field height=100%><TR><TD>".((substr_count($value,"\r\n")>$rows)?'<DIV style="overflow:auto; height:'.(15*$rows).'px; width:'.($cols*7).'; padding-right: 16px;border-bottom-style:dotted;border-bottom-width:1;border-bottom-color:#0033CC;">'.nl2br($value).'</DIV>':'<DIV style="overflow:auto; width:'.($cols*7).'; padding-right: 16px;border-bottom-style:dotted;border-bottom-width:1;border-bottom-color:#0033CC;">'.nl2br($value).'</DIV>').'</TD></TR></TABLE>'.(($title!='')?"<BR><small>".str_replace("'","&rsquo;",$title)."</small>":'')."</div></DIV>";
	}
	else
		return (($value!='')?$value:'-').(($title!='')?"<BR><small>$title</small>":'');
}

/**
* Create a clickable checkbox
*
* @param string $value initial value of the checkbox (Y if checked, blank of N if unchecked)
* @param string $name name of the HTML form field
* @param string $title optional title that is displayed under the field
* @param string $checked deprecated field
* @param string $new If true, the input field will be displayed by default (without the user clicking on the field). If false, the appropriate passed to either the $yes argument or the $no argument will be displayed.
* @param string $yes The value that should be displayed when the checkbox is checked.
* @param string $no The value that should be displayed when the checkbox is unchecked
* @param string $div If false, display HTML field by default (without forcing the user to click the field to edit it)
* @param string $extra optional string that will be added to the <INPUT> tag
* @return string HTML code for clickable checkbox field
*/
function CheckboxInput($value,$name,$title='',$checked='',$new=false,$yes='Yes',$no='No',$div=true,$extra='')
{	global $_FOCUS;

	if($yes=='Yes')
		$yes = _('Yes');
	if($no=='No')
		$no = _('No');

	// $checked has been deprecated -- it remains only as a placeholder
	if(Preferences('HIDDEN')!='Y')
		$div = false;

	if($div==false || $new==true)
	{
		if($value && $value!='N')
			$checked = 'CHECKED';
		else
			$checked = '';
	}

	if(AllowEdit() && !$_REQUEST['_FOCUS_PDF'])
	{
		$comment = '<!-- '.$value.' -->';
		if($new || $div==false)
			return $comment."<INPUT type=hidden name=$name value=\"\"><INPUT type=checkbox name=$name value=Y id=input$name $checked $extra>".(($title!='')?"<BR><small>$title</small>":'');
		else
			return $comment."<DIV id='div$name'><div id='innerdiv$name' onclick='javascript:addHTML(\"<INPUT type=hidden name=$name value=\\\"\\\"><INPUT type=checkbox id=input$name name=$name ".(($value && $value!='N')?'checked':'')." value=Y ".str_replace('"','\"',$extra).">".(($title!='')?"<BR><small>".str_replace("'","&rsquo;",$title)."</small>":'')."\",\"div$name\",true);doOnresize();'><span style='border-bottom-style:dotted;border-bottom-width:1;border-bottom-color:#0033CC;'>".(($value && $value!='N')?$yes:$no).'</span>'.(($title!='')?"<BR><small>".str_replace("'","&rsquo;",$title)."</small>":'')."</div></DIV>";
	}
	else
		return (($value && $value!='N')?$yes:$no).(($title!='')?"<BR><small>$title</small>":'');
}

/**
* Create a clickable pull-down menu
*
* @param string $value initial option to be selected in the <SELECT> menu (blank if N/A or the first option should be selected)
* @param string $name name of the HTML form field
* @param string $title optional title that is displayed under the field
* @param string $options optional set of options that are embedded into the <INPUT> tag
* @param string $allow_na The title of the first option in the list -- the option that should be selected by default if the user hasn't yet selected any option.  If this is set to false, the user is not allowed to select N/A or any blank value.
* @param string $div If false, the pulldown menu will be displayed by default (without the user clicking on the field). If true, the field will have to be clicked on before it can be edited unless $value is blank.  If set to 'force', the field will have to be clicked on even if blank (blank will be translated to a dash "-") 
* @return string HTML code for clickable text field
*/
function SelectInput($value,$name,$title='',$options='',$allow_na='',$extra='',$div=true)
{	global $_FOCUS;

	if(Preferences('HIDDEN')!='Y')
		$div = false;

	// mab - support array style $option values
	// mab - append current val to select list if not in list
	if ($value!='' && is_array($options) && !$options[$value])
		$options += array($value=>array($value,'<FONT color=#FF0000>'.$value.'</FONT>'));

	if(AllowEdit() && !$_REQUEST['_FOCUS_PDF'])
	{
		$comment = '<!-- '.$value.' -->';
		$return = $comment;
		if(($value!='' || $div==='force') && $div)
		{
			$return .= "<DIV id='div$name'><div id='innerdiv$name' onclick='javascript:addHTML(\"";
			$extra = str_replace('"','\"',$extra);
		}

		$return .= "<SELECT name=$name id=input$name $extra>";
		if($allow_na!==false)
		{
			if(!$allow_na)
				$allow_na = _('N/A');

			if(($value!='' || $div==='force') && $div)
				$return .= '<OPTION value=\"\">'.$allow_na;
			else
				$return .= '<OPTION value="">'.$allow_na;
		}
		if(count($options))
		{
			foreach($options as $key=>$val)
			{
				$key = (string) $key;
				if(($value!='' || $div==='force') && $div)
					$return .= "<OPTION value=\\\"".str_replace("'",'&rsquo;',$key)."\\\" ".(($value==$key && (!($value==false && ($value!==$key)) || ($value==='0' && $key===0)))?'SELECTED':'').">".str_replace("'",'&rsquo;',(is_array($val)?$val[0]:$val));
				else
					$return .= "<OPTION value=\"$key\" ".(($value==$key && !($value==false && $value!==$key))?'SELECTED':'').">".(is_array($val)?$val[0]:$val);
			}
		}
		$return .= "</SELECT>";
		if($title!='')
			$return .= "<BR><small>$title</small>";
		if(($value!='' || $div==='force') && $div)
			$return .="\",\"div$name\",true);doOnresize();'><span style='border-bottom-style:dotted;border-bottom-width:1;border-bottom-color:#0033CC;'>".(((is_array($options[$value])?$options[$value][1]:$options[$value])!='')?(is_array($options[$value])?$options[$value][1]:$options[$value]):'-').'</span>'.(($title!='')?"<BR><small>$title</small>":'').'</div></DIV>';
	}
	else
		$return = (((is_array($options[$value])?$options[$value][1]:$options[$value])!='')?(is_array($options[$value])?$options[$value][1]:$options[$value]):'-').(($title!='')?"<BR><small>$title</small>":'');

	return $return;
}

/**
* Create a clickable date input field
*
* This function uses PrepareDate() to create the HTML <SELECT> form tags.
*
* @param string $value initial value in the date field (blank if no initial value)
* @param string $name name suffix of the HTML form fields.  The HTML fields will actually have a name of year_$name, month_$name, and day_$name.
* @param string $title optional title that is displayed under the field
* @param string $div If false, the input fields will be displayed by default (without the user clicking on the field). If true, the field will have to be clicked on before it can be edited unless $value is blank.  If set to 'force', the field will have to be clicked on even if blank (blank will be translated to a dash "-") 
* @param string $allow_na The title of the first option in the list -- the option that should be selected by default if the user hasn't yet selected any option.  If this is set to false, the user is not allowed to select N/A or set a blank date.
* @return string HTML code for clickable text field
*/
function DateInput($value,$name,$title='',$div=true,$allow_na=true)
{	global $_FOCUS;

	if(Preferences('HIDDEN')!='Y')
		$div = false;
	if($value==DBDate())
		$big_year_range = true;
	else
		$big_year_range = false;

	if(AllowEdit() && !$_REQUEST['_FOCUS_PDF'])
	{
		if(($value=='' && $div!=='force') || $div==false)
			return PrepareDate($value,"_$name",$allow_na,array('big_year_range'=>$big_year_range))."<BR><small>$title</small>";
		else
			return "<DIV id='div$name'><div onclick='javascript:addHTML(\"".str_replace('"','\"',PrepareDate($value,"_$name",true,array('Y'=>1,'M'=>1,'D'=>1,'big_year_range'=>$big_year_range)))."<BR><small>$title</small>\",\"div$name\",true);doOnresize();'><span style='border-bottom-style:dotted;border-bottom-width:1;border-bottom-color:#0033CC;'>".(($value!='')?ProperDate($value):'-')."</span><BR><small>$title</small></div></DIV>";
	}
	else
		return (($value!='')?ProperDate($value):'-')."<BR><small>$title</small>";
}

/**
* Create a non-clickable text block with label that mimics the appearance of the Input fields created by TextInput, SelectInput, etc.
*
* @param string $value text value to be displayed above a label
* @param string $title the label displayed below the text
* @return string HTML code for the text and label
*/
function NoInput($value,$title='')
{
	return ($value!=''?$value:'-').($title!=''?'<BR><small>'.$title.'</small>':'');
}

?>